package com.example.sunmiv1.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.VipListBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChooseCouponAdapter extends BaseAdapter {

    private List<VipListBean.InfoBean.CouponListBean> list;
    private ViewHolder viewHolder;
    private int select;

    public ChooseCouponAdapter(List<VipListBean.InfoBean.CouponListBean> list) {
        this.list = list;
    }

    public void setSelect(int select) {
        this.select = select;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.choose_coupon_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        /*if ("1".equals(list.get(position).getGoods_type()) || TextUtils.isEmpty(list.get(position).getGoods_type())) {
            viewHolder.tvCouponName.setText(parent.getContext().getString(R.string.satisfy) + list.get(position).getNeed_money() + parent.getContext().getString(R.string.couponMoney) + list.get(position).getReduce_money());
        } else {
            viewHolder.tvCouponName.setText(parent.getContext().getString(R.string.satisfy) + list.get(position).getNeed_money() + parent.getContext().getString(R.string.couponMoney) +
                    list.get(position).getReduce_money() + "(指定商品)");
        }*/
        if (select == position) {
            viewHolder.couponAdaImg.setImageResource(R.mipmap.order_sheet_vip_coupon_x);
        } else {
            viewHolder.couponAdaImg.setImageResource(R.mipmap.order_sheet_vip_coupon_wx);
        }

        if (list.get(position).getCoupon_choice_type() == 2) {//
            viewHolder.couponAdaType.setText("會員優惠券");
        } else if (list.get(position).getCoupon_choice_type() == 3) {
            viewHolder.couponAdaType.setText("微信商家券");
        }
        viewHolder.couponAdaName.setText(list.get(position).getCoupon_name());
        viewHolder.couponAdaFull.setText("滿" + list.get(position).getNeed_money() + "元減" + list.get(position).getReduce_money());

        if ("1".equals(list.get(position).getGoods_type()) || TextUtils.isEmpty(list.get(position).getGoods_type())) {
            viewHolder.couponAdaLimit.setText("所有商品可用");
            viewHolder.couponAdaDetailLin.setVisibility(View.GONE);
        } else if ("2".equals(list.get(position).getGoods_type()) || TextUtils.isEmpty(list.get(position).getGoods_type())) {
            viewHolder.couponAdaLimit.setText("指定商品可用");
            viewHolder.couponAdaDetailLin.setVisibility(View.VISIBLE);
        } else if ("3".equals(list.get(position).getGoods_type()) || TextUtils.isEmpty(list.get(position).getGoods_type())) {
            viewHolder.couponAdaLimit.setText("指定分類可用");
            viewHolder.couponAdaDetailLin.setVisibility(View.VISIBLE);
        }
        if (TextUtils.isEmpty(list.get(position).getAppoint_type_name())) {
            viewHolder.couponAdaDetail.setText("");
        } else {
            viewHolder.couponAdaDetail.setText(list.get(position).getAppoint_type_name());
        }
        final LinearLayout detailLin = viewHolder.couponAdaDetailLin;
        final TextView detail = viewHolder.couponAdaDetail;
        detailLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (detail.getVisibility() == View.GONE) {
                    detail.setVisibility(View.VISIBLE);
                } else {
                    detail.setVisibility(View.GONE);
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.couponAda_img)
        ImageView couponAdaImg;
        @BindView(R.id.couponAda_name)
        TextView couponAdaName;
        @BindView(R.id.couponAda_type)
        TextView couponAdaType;
        @BindView(R.id.couponAda_full)
        TextView couponAdaFull;
        @BindView(R.id.couponAda_limit)
        TextView couponAdaLimit;
        @BindView(R.id.couponAda_detailLin)
        LinearLayout couponAdaDetailLin;
        @BindView(R.id.couponAda_detail)
        TextView couponAdaDetail;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
